<template>
  <!-- 资产品牌表单弹窗 -->
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    width="400px"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="90">
      <el-row>
        <el-col :span="24">
          <el-form-item label="制造商" prop="makerId">
            <el-select
              v-model="formData.makerId"
              placeholder="请选择制造商"
              clearable
              v-if="!props.isReadonly"
              style="width: 300px"
            >
              <el-option
                v-for="item in makerList"
                :key="`${item.id}`"
                :label="`${item.makerCode}-${item.makerName}`"
                :value="`${item.id}`"
              />
            </el-select>

            <span v-else>{{ formData.makerName }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="品牌编号" prop="brandCode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.brandCode"
              placeholder="请输入品牌编号"
              clearable
            />

            <span v-else>{{ formData.brandCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="品牌名称" prop="brandName">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.brandName"
              placeholder="请输入品牌名称"
              clearable
            />

            <span v-else>{{ formData.brandName }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="启用状态" prop="enabled">
            <el-radio-group v-model="formData.enabled" v-if="!props.isReadonly">
              <el-radio :label="1">启用</el-radio>
              <el-radio :label="0">停用</el-radio>
            </el-radio-group>

            <span v-else>{{ formData.enabled == 1 ? "启用" : "停用" }}</span>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "EAMEAMASSETBRANDEDIT",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  assetKind: {
    type: Number,
    default: null,
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);
watch(
  () => props.assetKind,
  (newVal: number) => {},
  {
    immediate: true,
  }
);
const rules = reactive({
  brandCode: [{ required: true, message: "请输入品牌编号", trigger: "blur" }],
  brandName: [{ required: true, message: "请输入品牌名称", trigger: "blur" }],
  makerId: [{ required: true, message: "请选择制造商", trigger: "change" }],
});

const formRef = ref(ElForm);

let formData = reactive({
  assetKind: props.assetKind,
  id: "",
  version: "",
  brandCode: "",
  brandName: "",
  makerId: "",
  makerName: "",
  remarks: "",
  organizationId: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  enabled: 1,
  deleted: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        assetKind,
        id,
        version,
        brandCode,
        brandName,
        makerId,
        makerName,
        remarks,
        organizationId,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
      } = newVal;
      Object.assign(formData, {
        assetKind,
        id,
        version,
        brandCode,
        brandName,
        makerId,
        makerName,
        remarks,
        organizationId,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.assetKind = props.assetKind;
  formData.id = "";
  formData.version = "";
  formData.brandCode = "";
  formData.brandName = "";
  formData.makerId = "";
  formData.makerName = "";
  formData.remarks = "";
  formData.organizationId = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.enabled = 1;
  formData.deleted = "";
}

const loading = ref(false);

/** 资产品牌保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/eam/eamassetbrand/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}
// 获取制造商列表
const makerList: any = ref([]);
const getMakerList = async () => {
  request({
    url: `/eam/eammaker/getList`,
    method: "post",
    data: { assetKind: props.assetKind },
  })
    .then((res) => {
      makerList.value = res.data;
    })
    .catch((res) => {
      console.log(res);
    });
};
// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
onMounted(() => {
  getMakerList();
});
</script>
